<template>
	<view class="content">
		<!-- <view class="help-pubuBox">
			<u-search placeholder="搜索风险单元" :clearabled="true" height="40" v-model="title" @search="chaxun" color="#2B2B2B"
				@custom="$dianji(chaxun,'onoff')" shape="square" :actionText="'搜索'" :inputStyle="inputStyle" @clear="cancel"
				:actionStyle="actionStyle"></u-search>
		</view> -->
		<view class="user-box">
			<m-tree ref="mtree" :edit="true" :defaultProps="defaultProps" :data="tree" :divider="true" :unfold="true"
				@node-click="nodeClick" @editItem="editItem">
			</m-tree>
		</view>
		<view class="empty-data" v-if="tree.length==0">
			<u-empty mode="data" :marginTop="100"></u-empty>
		</view>

		<!-- 底部按钮 -->
		<view class="xing-subside"></view>
		<view class="xing-bottom-double">
			<view class="btn-left" @click="$dianji(back,'onoff1')">返回</view>
			<view class="btn-right" :class="[checkItem.checked?'disabledItem':'']" @click="$dianji(sumbit,'onoff')">确定
			</view>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				onoff: true,
				onoff1: true,
				//搜索值
				title: '',
				//按钮样式
				actionStyle: {
					color: '#0F53AC',
					fontSize: "32rpx",
				},
				inputStyle: {
					fontSize: '32rpx',
				},
				defaultProps: {
					id: 'id', // 此项为id项的key
					children: 'children', // 此项为修改子集数据的key
					label: 'configValue' // 此项为修改显示数据的key
				},
				params: {
					configType: 'risk_unit',
					keyword: '',
				},
				tree: [],
				checkItem: {
					checked: true
				}
			}
		},
		onLoad() {
			this.getUnitData()
		},
		methods: {
			//2.搜索功能
			//change
			chaxun() {
				this.params.keyword = this.title
				this.getUnitData()
			},
			//cancel
			cancel() {
				this.title = ''
				this.params.keyword = ''
				this.getUnitData()
			},
			getUnitData() {
				uni.$u.http.post(`/check/mobile/dataDictionary/dictionaryList`, this.params).then(res => {
					if (res.code == 0) {
						let newData = this.format(res.data)
						this.tree = newData
					}
				}).catch(err => {})
			},
			format(arr) {
				arr.map(item => {
					item.checked = false
					if (!item.children) {
						item.children = []
					} else if (item.children) {
						item.children = this.format(item.children)
					}
				})
				return arr
			},
			nodeClick(e) {
				// if (!e.children.length) {
				// 	uni.$emit('setUnit', e)
				// 	uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				// 		delta: 1
				// 	});
				// }
			},
			editItem(e, checked) {
				this.checkItem = e
				this.fixMultiple(this.tree)
				this.setStatus(this.tree, e, checked)
			},
			setStatus(arr, item, checked) {
				arr.forEach(it => {
					if (it.id == item.id) {
						it.checked = checked
						return
					} else if (it.children && it.children.length) {
						this.setStatus(it.children, item, checked)
					}
				})
			},
			// 重置tree
			fixMultiple(arr) {
				arr.forEach(item => {
					item.checked = false
					if (item.children && item.children.length) {
						this.fixMultiple(item.children)
					}
				})
			},
			back() {
				uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			},
			sumbit() {

				if (this.checkItem.checked) {
					return
				} else {
					uni.$emit('setUnit', this.checkItem)
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: 1
					});
				}

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.help-pubuBox {
		padding: 22rpx;
		background: #fff;
	}

	.user-box {
		border-top: 1px solid #f2f2f2;
		background-color: #fff;
	}

	.disabledItem {
		opacity: 0.5;
	}
</style>
